/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecomp.uefs.micc.sharedbox.model;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

import javax.swing.JOptionPane;

/**
 *
 * @author emanuel
 */
public class OtherClass extends Thread {

    private Socket client;
    private User user;

    public OtherClass(Socket client) {
    	this.client = client;
    	user = new User();
    }

    @Override
    public void run() {

        try {

            //ObjectInputStream para receber o nome do arquivo
        	

            ObjectInputStream input = new ObjectInputStream(client.getInputStream());

            DataOutputStream output = new DataOutputStream(client.getOutputStream());

            //Recebe o nome do arquivo

            String arquivo = (String) input.readObject();

            //Buffer de leitura dos bytes do arquivo

            byte buffer[] = new byte[512];

            //Leitura do arquivo solicitado

            FileInputStream file = new FileInputStream(arquivo);

            //DataInputStream para processar o arquivo solicitado

            DataInputStream arq = new DataInputStream(file);

            output.flush();

            int leitura = arq.read(buffer);

            //Lendo os bytes do arquivo e enviando para o socket     

            while (leitura != - 1) {

                if (leitura != - 2) {

                    output.write(buffer, 0, leitura);

                }

                leitura = arq.read(buffer);

            }

            System.out.println("Cliente atendido com sucesso: " + arquivo
                    + client.getRemoteSocketAddress().toString());

            input.close();
           
            output.close();

            client.close();

        } catch (Exception e) {

            System.out.println("Excecao ocorrida na thread: " + e.getMessage());

            try {

                client.close();

            } catch (Exception ec) {
            }

        }

    }    
          
    
    public void setUser(){
    	this.user = user;
    }
    
    public User getUser(){
    	
    	return user;
    }
}
